package com.czmh.etc.onlinepubsys.controller.mingchuang;

import com.alibaba.fastjson.JSON;
import com.czmh.etc.onlinepubsys.constants.mingchuang.BusinessType;
import com.czmh.etc.onlinepubsys.entity.mingchuang.ApplyInfo;
import com.czmh.etc.onlinepubsys.entity.mingchuang.CurrentContext;
import com.czmh.etc.onlinepubsys.json.mingchuang.JSONResult;
import com.czmh.etc.onlinepubsys.request.mingchuang.AbcAndCcbBankSignConfirmRequest;
import com.czmh.etc.onlinepubsys.request.mingchuang.BankActiveConfirmRequest;
import com.czmh.etc.onlinepubsys.request.mingchuang.BankMsgApplyRequest;
import com.czmh.etc.onlinepubsys.request.mingchuang.ICBCBankSignConfirmRequest;
import com.czmh.etc.onlinepubsys.services.mingchuang.ICurrentContextService;
import com.czmh.etc.onlinepubsys.services.mingchuang.IEtcApplyService;
import com.czmh.etc.onlinepubsys.utils.mingchuang.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/{version}/api/mingchang/bank")
@Api(tags = "银行接口【新】- 铭创")
public class BankController {
    @Autowired
    private ICurrentContextService currentContextService;
    @Autowired
    private IEtcApplyService etcApplyService;

    @PostMapping("/messageSignApply")
    @ApiOperation("短信签约申请 6021")
    public JSONResult messageSignApply(@RequestBody BankMsgApplyRequest req) {
        log.info("短信签约申请:{}", JSON.toJSONString(req));
        JSONResult result = HttpUtil.post(BusinessType.BANK_MSG_SIGN, req);
        if (result.getCode().equals(JSONResult.SUCCESS)) {
            CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
            ApplyInfo applyInfo = etcApplyService.get(ctx.getApplyId());

            applyInfo.setBankCardNo(req.getBankCardNo());
            applyInfo.setBankSn(req.getChannelType());
            applyInfo.setBankCardType(req.getBankCardType());
            etcApplyService.update(applyInfo);
        }
        return result;
    }

    @PostMapping("/abcAndBbcSignConfirm")
    @ApiOperation("农行/建行签约确认 6022")
    public JSONResult abcAndBbcSignConfirm(@RequestBody AbcAndCcbBankSignConfirmRequest req) {
        log.info("农行/建行签约确认:{}", JSON.toJSONString(req));
        JSONResult result = HttpUtil.post(BusinessType.ABC_CCB_BANK_SIGN_CONFIRM, req);
        if (result.getCode().equals(JSONResult.SUCCESS)) {
            String signChannelId = JSON.parseObject(result.getRspData()).getString("signChannelId");
            CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
            log.info("短信签约确认签约成功,保存资金账户ID:{}", signChannelId);

            //更新进行时
            ctx.setSignChannelId(signChannelId);
            ctx.setTransNo(req.getTransNo());
            ctx.setState(4); //已银行短信签约
            currentContextService.update(ctx);

            ApplyInfo applyInfo = etcApplyService.get(ctx.getApplyId());
            applyInfo.setBankSignTime(new Date());
            applyInfo.setState(1);
            etcApplyService.update(applyInfo);
        }
        return result;
    }

    @PostMapping("/icbcSignConfirm")
    @ApiOperation("工行签约确认接口6025")
    public JSONResult icbcSignConfirm(@RequestBody ICBCBankSignConfirmRequest req) {
        log.info("工行签约确认:{}", JSON.toJSONString(req));
        JSONResult result = HttpUtil.post(BusinessType.ICBC_BANK_SIGN_CONFIRM, req);
        if (result.getCode().equals(JSONResult.SUCCESS)) {
            String signChannelId = JSON.parseObject(result.getRspData()).getString("signChannelId");
            CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
            log.info("短信签约确认签约成功,保存资金账户ID:{}", signChannelId);

            //更新进行时
            ctx.setSignChannelId(signChannelId);
            ctx.setState(4); //已银行短信签约
            currentContextService.update(ctx);

            ApplyInfo applyInfo = etcApplyService.get(ctx.getApplyId());
            applyInfo.setBankSignTime(new Date());
            applyInfo.setState(1);
            applyInfo.setBankCardNo(req.getBankCardNo());
            applyInfo.setBankSn("ICBC");
            applyInfo.setBankCardType(1);
            etcApplyService.update(applyInfo);
        }
        return result;
    }

    @PostMapping("/activeConfirm")
    @ApiOperation("银行签约确认 1071")
    public JSONResult activeConfirm(@RequestBody BankActiveConfirmRequest req) {
        log.info("银行签约确认:{}", JSON.toJSONString(req));
        JSONResult result = HttpUtil.post(BusinessType.BANK_ACTIVE_CONFIRM, req);
        if (result.getCode().equals(JSONResult.SUCCESS)) {
            CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
            ctx.setState(4);
            currentContextService.update(ctx);
        }
        return result;
    }
}
